package com.hssy.wululuaplan.controller;

import com.hssy.wululuaplan.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "测试", description = "前期测试用，后期需删除整个控制器")
@RequestMapping("/test")
public class TestController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("测试")
    @GetMapping("/helloWorld")
    public Result<String> helloWorld(){
        return Result.success("你好");
    }

    @ApiOperation("测试发送到Redis")
    @PostMapping("/sendRedis")
    public Result<String> sendRedis(){
        stringRedisTemplate.opsForValue().set("test","这是一段测试！");
        return Result.success();
    }

    @ApiOperation("测试查看Redis消息")
    @GetMapping("/selectRedis")
    public Result<String> selectRedis(){
        String test = stringRedisTemplate.opsForValue().get("test");
        return Result.success(test);
    }

    @ApiOperation("算数异常模拟")
    @GetMapping("/arithmeticException")
    public Result<String> arithmeticException(){
        int a = 4 /0 ;
        return Result.success();
    }

    @ApiOperation("全局异常模拟")
    @GetMapping("/exception")
    public Result<String> exception(){
        List<Map<String,String>> list = new ArrayList<>();
        return Result.success(list.get(2).get("test"));
    }

}
